<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>按钮一</button>
    <button>按钮二</button>
    <script>
      var btn = document.querySelectorAll("button");

      function debounce1(fn, delay) {
        var timer = null;
        return function () {
          if (!timer) {
            fn();
          }
          clearTimeout(timer);
          timer = setTimeout(function () {
            timer = null;
          }, delay);
        };
      }
      btn[0].onclick = debounce1(function () {
        console.log(1);
      }, 1000);

      function debounce2(fn, delay) {
        var timer;
        return function () {
          clearTimeout(timer);
          timer = setTimeout(function () {
            fn();
          }, delay);
        };
      }

      btn[1].onclick = debounce2(function () {
        console.log(2);
      }, 1000);
    </script>
  </body>
</html>
